home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / CDROM / CDPLAY.C next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  5.1 KB  |  222 lines

  1. /*                           CDROM AUDIO PLAYER
  2.                   By Barry Egerter
  3.  
  4.                 Revised Sept 28, 1994
  5.  
  6.                Using Watcom C/C++ 10.0
  7.  
  8.          Code and program: FREEWARE - alter and use at will.
  9.  
  10.  
  11.            Internet Email:      barry.egerter@softnet.com
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <time.h>
  17. #include <conio.h>
  18. #include "cdrom.h"
  19. #include <wgt5.h>
  20. #include <wgtvesa.h>
  21. /******************************PLAYER PROGRAM*******************************/
  22.  
  23. #define STOPPED 0
  24. #define PLAYING 1
  25. #define PAUSED  2
  26. #define RANDOM  3
  27.  
  28. #define TRAY_OPEN   1
  29. #define TRAY_CLOSED 2
  30.  
  31. /* VARIABLES GLOBAL TO TEST PROGRAM */
  32. int startpos, endpos;  
  33. short music;
  34. unsigned char min, sec, frame;
  35. short tracks;
  36. short traystatus;
  37. char outstring[60];
  38. char track1[5];
  39. char track2[5];
  40.  
  41. block digits[20];
  42. color pal[256];
  43.  
  44.  
  45. void showtime (short x, short y, char *output)
  46. {
  47.   short len;
  48.   short ctr;
  49.   char spr;
  50.  
  51.   len = strlen (output);
  52.   for (ctr = 0; ctr < len; ctr++)
  53.   {
  54.     if (outstring[ctr] == ':')    
  55.       spr = 10;
  56.     else spr = (outstring[ctr]) - 48;
  57.     wvesa_putblock (x, y, digits[spr], NORMAL);
  58.     x += 11;
  59.   }
  60. }
  61.  
  62.  
  63.  
  64. void get_musicpos ()
  65. {
  66.   short temp;
  67.  
  68.   startpos = cdrom_data.track_position;
  69.   temp = cdrom_data.current_track;
  70.   if ((temp+1) > cdrom_data.high_audio)
  71.     endpos = cdrom_data.endofdisk;
  72.   else 
  73.   {
  74.     cd_set_track (temp+1);
  75.     endpos = cdrom_data.track_position;
  76.     cd_set_track (temp);
  77.   }
  78. }
  79.  
  80.  
  81. void main (void)
  82. {
  83.   short temp;
  84.   short row;
  85.   char ch;
  86.   short oldmode;
  87.   struct playinfo songdata;  
  88.  
  89.   srand (time (NULL));
  90.   if (!cdrom_installed ())
  91.   {
  92.     printf ("No CDROM detected.\n");
  93.     exit (0);
  94.   }
  95.   if (!wvesa_detected ())
  96.   {
  97.     printf ("SVGA VESA driver not found!\n");
  98.     exit (0);
  99.   }
  100.   oldmode = wgetmode ();
  101.   wsetmode (oldmode);
  102.   printf ("Before the program continues, make sure you have an audio CD in the drive\n");
  103.   printf ("and the tray is closed.\n\n\nPress any key to begin....\n");
  104.   getch ();
  105.   vga256 ();
  106.   wvesa_init (V640x400);
  107.   wloadsprites (pal, "clock.spr", digits, 0, 20);
  108.   wsetpalette (0, 255, pal);
  109.   tracks = cdrom_data.high_audio - cdrom_data.low_audio + 1;
  110.   row = 0;  
  111.   wtextcolor (15);
  112.   wtextbackground (TEXTFGBG);
  113.   for (temp = cdrom_data.low_audio; temp <= cdrom_data.high_audio; temp++)
  114.   {
  115.     cd_track_length (temp, &min, &sec, &frame);
  116.     sprintf (&outstring, "%2d      %02d:%02d:%02d", temp, min, sec, frame);
  117.     showtime (0, row, outstring);
  118.     cd_set_track (temp);
  119.     if (cdrom_data.track_type == DATA_TRACK)
  120.       wvesa_outtextxy (34, row+2, NULL, "DATA");
  121.     else
  122.       wvesa_outtextxy (34, row+2, NULL, "AUDIO");
  123.     row += 14;
  124.   }
  125.   wtextcolor (27);
  126.   wvesa_outtextxy (400, 0, NULL, "F1-F10  Selects Track");
  127.   wvesa_outtextxy (400, 10, NULL, "P       Pauses/Resumes Play");
  128.   wvesa_outtextxy (400, 20, NULL, "SPACE   Stops Playback");
  129.   wvesa_outtextxy (400, 30, NULL, "R       Random Track");
  130.   wvesa_outtextxy (400, 40, NULL, "E       Eject Tray");
  131.   wvesa_outtextxy (400, 50, NULL, "C       Close Tray");
  132.   wvesa_outtextxy (400, 100, NULL, "ENTER   Quit CDPLAY");
  133.   music = STOPPED;
  134.   traystatus = TRAY_CLOSED;
  135.   do {
  136.     while ((!kbhit ()) && (traystatus == TRAY_CLOSED))
  137.     {
  138.       if ((cd_done_play ()) && (music != PAUSED))
  139.       {
  140.     wsetcolor (0);
  141.     wvesa_bar (200, 0, 300, 399);
  142.     if (music == RANDOM)
  143.     {
  144.       cd_set_track ((rand () % cdrom_data.high_audio) + 1);
  145.       if (cdrom_data.track_type != DATA_TRACK)
  146.       {
  147.         get_musicpos ();
  148.         cd_play_audio (startpos, endpos);
  149.         music = PLAYING;
  150.       }
  151.     } else music = STOPPED;
  152.       }
  153.  
  154.       if (music == PLAYING)
  155.       {
  156.     cd_getpos (&songdata);
  157.     sprintf (&outstring, "%02d:%02d:%02d", songdata.min, songdata.sec, songdata.frame);
  158.     sprintf (&track1, "%02x", songdata.track);
  159.     sprintf (&track2, "%02d", cdrom_data.current_track);
  160.     if (strcmp (track1, track2) == 0)
  161.       showtime (200, (cdrom_data.current_track - 1) * 14, outstring);
  162.       }
  163.     }  
  164.     ch = toupper (getch ());
  165.     if (ch == 0)
  166.     {
  167.       ch = toupper (getch ());
  168.       if ((ch < 69) && (ch > 58) && (ch-58 <= cdrom_data.high_audio))
  169.       {
  170.     cd_stop_audio ();
  171.     wsetcolor (0);
  172.     wvesa_bar (200, 0, 300, 399);
  173.     cd_set_track (ch-58);
  174.     if (cdrom_data.track_type != DATA_TRACK)
  175.     {
  176.       get_musicpos ();
  177.       music = PLAYING;
  178.       cd_play_audio (startpos, endpos);
  179.     }
  180.       }
  181.     }
  182.     
  183.     if ((ch == 'E') && (music == STOPPED))
  184.     {
  185.       cd_cmd (EJECT_TRAY);
  186.       traystatus = TRAY_OPEN;
  187.     }
  188.     
  189.     if ((ch == 'C') && (music == STOPPED))
  190.     {
  191.       cd_cmd (CLOSE_TRAY);
  192.       traystatus = TRAY_CLOSED;
  193.     }
  194.     
  195.     if (ch == ' ')
  196.     {
  197.       cd_stop_audio ();
  198.       music = STOPPED;
  199.     }
  200.  
  201.     if ((ch == 'R') && (traystatus == TRAY_CLOSED))
  202.     {
  203.       cd_stop_audio ();
  204.       music = RANDOM;
  205.     }
  206.  
  207.     if ((ch == 'P') && (music != STOPPED))
  208.      if (music == PAUSED)
  209.      {
  210.        cd_resume_audio ();
  211.        music = PLAYING;
  212.      }
  213.      else
  214.      {
  215.        cd_stop_audio ();
  216.        music = PAUSED;
  217.      }
  218.   } while (ch != 13);
  219.   cd_stop_audio ();
  220.   wsetmode (oldmode);
  221. }  
  222.